package com.neu.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.neu.R;
import com.neu.bean.Student;

import org.litepal.LitePal;

import java.util.List;

public class StudentActivity extends AppCompatActivity {

    private EditText name;
    private EditText snum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student);
        //初始化Litpal
        LitePal.initialize(this);

        name=findViewById(R.id.name);
        snum=findViewById(R.id.snum);
        name.setText("张三");
        snum.setText("456");
    }

    //添加
    public void btnAdd(View view) {
        Student stu=new Student();
        stu.setName(name.getText().toString());
        stu.setSnum(Integer.parseInt(snum.getText().toString()));
        if(stu.save()){
            Toast.makeText(this,"插入成功",Toast.LENGTH_LONG).show();
        }else {
            Toast.makeText(this, "失败了。。。。。。", Toast.LENGTH_LONG).show();
        }

    }
    //更新
    public void btnUpdate(View view) {
       Student stu=LitePal.find(Student.class,2);
       stu.setName(stu.getName()+"修改了");
        if(stu.update(2)>0){
            Toast.makeText(this,"更新成功",Toast.LENGTH_LONG).show();
        }else {
            Toast.makeText(this, "失败了。。。。。。", Toast.LENGTH_LONG).show();
        }
    }
    //删除
    public void btnDel(View view) {
        Student stu=LitePal.find(Student.class,2);

        if( stu.delete()>0) {
            Toast.makeText(this,"删除成功",Toast.LENGTH_LONG).show();
        }else {
            Toast.makeText(this, "失败了。。。。。。", Toast.LENGTH_LONG).show();
        }
    }
    //查询
    public void btnSelect(View view) {
        List<Student> list=LitePal.findAll(Student.class);
        for(Student s:list){
            Log.d("TAG", s.toString());
        }

    }
}